<%@ page contentType="image/jpeg"%><%
%><%@ page import="com.sun.image.codec.jpeg.*"%><%
%><%@ page import="org.jfree.chart.JFreeChart" %><%
%><%@ page import="java.awt.image.BufferedImage" %><%
%><%@ page import="BusinessLogic.ControllerFacade" %><%
%><%@ page import="org.jfree.chart.plot.CategoryPlot" %><%
%><%@ page import="org.jfree.data.category.DefaultCategoryDataset" %><%
%><%@ page import="org.jfree.chart.axis.CategoryAxis" %><%
%><%@ page import="org.jfree.chart.axis.NumberAxis" %><%
%><%@ page import="org.jfree.chart.axis.NumberTickUnit" %><%
%><%@ page import="org.jfree.chart.renderer.category.BarRenderer3D" %><%
%><%@ page import="Domain.DataStructures.IRecommendationData" %><%
%><%@ page errorPage="error.jsp" %><%

    ControllerFacade c = (ControllerFacade) session.getAttribute("c");
    String movieName = request.getParameter("movieName");

    int[] ranks = new int[10];
    for (Object rd : c.getRecommendationForMovie(movieName)) {
        ranks[(((IRecommendationData) rd).getUserRank()) - 1]++;
    }

    DefaultCategoryDataset cds = new DefaultCategoryDataset();

    int max = 3;
    for (int i = 0; i < 10; i++) {
        cds.setValue(ranks[i], "" + (i + 1), "" + (i + 1));
        if (max < ranks[i])
            max = ranks[i];
    }

    NumberAxis values = new NumberAxis("Number of recommendations");
    values.setTickUnit(new NumberTickUnit(1));
    values.setRange(0, max + 2);

    CategoryPlot p = new CategoryPlot(cds,
            new CategoryAxis("User rank"),
            values,
            new BarRenderer3D());

    JFreeChart jf = new JFreeChart("Ranks distribution for " + movieName, p);
    BufferedImage i = jf.createBufferedImage(600, 400);

    ServletOutputStream out1 = response.getOutputStream();  
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out1);
    encoder.encode(i);
%>